<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
	<div data-type="layout" data-defs="fit:true">
		<div data-defs="region:'west',title:'#springMessage('roleInfo')',border:false,bodyCls:'byr',headerCls:'byb byr',split:true" style="width: 300px;">
			<table id="authoritymanage_gridRole" data-type="grid" title="" style="height: 300px"
				data-defs="
				method:'post',url: '${rc.contextPath}/admin/queryRoleForAuthorityGrid.do',
				toolbar:'#orgTreeGridToolBar_authoritymanage',onSelect:rowSelectFun,singleSelect:true,
				rownumbers:true, pagination:false, fitColumns:true, fit:true">
				<thead>
					<tr>
						<th data-defs="field:'roleId',width:80,checkbox:true">ID</th>
						<th data-defs="field:'roleName',width:200">#springMessage('roleName')</th>
					</tr>
				</thead>
			</table>
			<div id="orgTreeGridToolBar_authoritymanage" style="height: auto">
				<a id="authoritymanage_authorityButton" href="javascript:void(0)" data-type="button"
					data-defs="iconCls:'icon-ok',plain:true">#springMessage('buttonAuthority')</a>
			</div>
		</div>
		<div data-defs="region:'center',split:true,border:false,bodyCls:'byl',headerCls:'byb byl'" title="#springMessage('resourceInfo')"
			style="width: 500px;">
			<div style="height: auto">
				<a id="authoritymanage_selectAllButton" href="javascript:void(0)" data-type="button"
					data-defs="iconCls:'icon-ok',plain:true">#springMessage('buttonAllSelect')</a>
				<a id="authoritymanage_reverseSelectAllButton" href="javascript:void(0)" data-type="button"
					data-defs="iconCls:'icon-ok',plain:true">#springMessage('buttonReverseSelect')</a>
			</div>
			<ul id="authoritymanage_treeResource" data-type="tree" style="padding: 2px"
				data-defs="
					method:'post',border:true,
					url:'${rc.contextPath}/admin/getResource.do',
					animate:false,dnd:false,checkbox:true"></ul>
		</div>
	</div>
	<script type="text/javascript">
		function rowSelectFun(index, rowData) {
	        var url = '${rc.contextPath}/admin/getRoleResources.do';
	        var data = {
		        "roleId" : rowData.roleId
	        };
	        ajaxPost(url, data, function(rs) {
		        var nodes_checked = $('#authoritymanage_treeResource').tree('getChecked');
		        for ( var i = 0; i < nodes_checked.length; i++) {
			        $('#authoritymanage_treeResource').tree("uncheck", nodes_checked[i].target);
		        }
		        if (rs != null) {
			        for ( var i = 0; i < rs.length; i++) {
				        var node = $('#authoritymanage_treeResource').tree("find", rs[i].resourceId);
				        $('#authoritymanage_treeResource').tree("check", node.target);
			        }
		        }
	        });
        }
        $(document).ready(function() {
	        $("#authoritymanage_selectAllButton").bind("click", function() {
		        var roots = $('#authoritymanage_treeResource').tree("getRoots");
		        for ( var i = 0; i < roots.length; i++) {
			        $('#authoritymanage_treeResource').tree("check", roots[i].target);
		        }
	        });
	        $("#authoritymanage_reverseSelectAllButton").bind("click", function() {
		        var nodes_checked = $('#authoritymanage_treeResource').tree('getChecked');
		        var nodes_unchecked = $('#authoritymanage_treeResource').tree('getChecked', 'unchecked');
		        for ( var i = 0; i < nodes_checked.length; i++) {
			        $('#authoritymanage_treeResource').tree("uncheck", nodes_checked[i].target);
		        }
		        for ( var i = 0; i < nodes_unchecked.length; i++) {
			        $('#authoritymanage_treeResource').tree("check", nodes_unchecked[i].target);
		        }
	        });
	        $("#authoritymanage_authorityButton").bind("click", function() {
		        var roleRows = $('#authoritymanage_gridRole').grid("getChecked");
		        if (roleRows == null || roleRows.length == 0) {
			        showMsg("#springMessage('unSelectedMsg')");
			        return;
		        }
		        var authoritymanage_resourceTreeNodes = $('#authoritymanage_treeResource').tree("getChecked");
		        if (authoritymanage_resourceTreeNodes != null && authoritymanage_resourceTreeNodes.length > 0) {
			        authoritymanage_resourceTreeNodes = JSON.stringify(authoritymanage_resourceTreeNodes);
		        } else {
			        authoritymanage_resourceTreeNodes = "";
		        }
		        var url = '${rc.contextPath}/admin/saveRoleResource.do';
		        var data = {
		            "roleId" : roleRows[0].roleId,
		            "authoritymanage_resourceTreeNodes" : authoritymanage_resourceTreeNodes
		        };
		        ajaxPost(url, data, function(rs) {
			        showMsg(rs.msgInfo);
		        });
	        });
        });
	</script>
</body>
</html>